GOHOSTOS:=$(shell go env GOHOSTOS)
APP_NAME=$(ROOT_PACKAGE)

ifeq ($(GOHOSTOS), windows)
	#the `find.exe` is different from `find` in bash/shell.
	#to see https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/find.
	#changed to use git-bash.exe to run find cli or other cli friendly, caused of every developer has a Git.
	#Git_Bash= $(subst cmd\,bin\bash.exe,$(dir $(shell where git)))
	Git_Bash=$(subst \,/,$(subst cmd\,bin\bash.exe,$(dir $(shell where git | grep cmd))))
	ALL_PROTO_FILES=$(shell $(Git_Bash) -c "find api -name *.proto")
	INTERNAL_PROTO_FILES=$(shell $(Git_Bash) -c "find internal/$(APP_NAME) -name *.proto")
	API_PROTO_FILES=$(shell $(Git_Bash) -c "find api/$(APP_NAME) -name *.proto")
	ERROR_PROTO_FILES=$(shell $(Git_Bash) -c "find api/$(APP_NAME) -name error_*.proto")
else
	ALL_PROTO_FILES=$(shell find api -name *.proto)
	INTERNAL_PROTO_FILES=$(shell find internal/$(APP_NAME) -name *.proto)
	API_PROTO_FILES=$(shell find api/$(APP_NAME) -name *.proto)
	ERROR_PROTO_FILES=$(shell find api/$(APP_NAME) -name error_*.proto)
endif

## init: init env
.PHONY: init
init:
	go install google.golang.org/protobuf/cmd/protoc-gen-go@latest
	go install github.com/google/gnostic/cmd/protoc-gen-openapi@latest
	go install github.com/google/wire/cmd/wire@latest
	go install github.com/favadi/protoc-go-inject-tag@latest
	go install github.com/pseudomuto/protoc-gen-doc/cmd/protoc-gen-doc@latest

## api: generate api proto
.PHONY: api
api: tools.verify.protoc-gen-go tools.verify.protoc-gen-go-gin tools.verify.protoc-gen-openapi tools.verify.protoc-go-inject-tag
	protoc --proto_path=./api \
	       --proto_path=./third_party \
 	       --go_out=paths=source_relative:./api \
 	       --go-gin_out=paths=source_relative:./api \
	       $(API_PROTO_FILES)
	protoc-go-inject-tag -input="./api/$(APP_NAME)/v1/*.pb.go"

## generate: go generate
.PHONY: generate
generate: tools.verify.wire
	go mod tidy
	go get github.com/google/wire/cmd/wire@latest
	go generate ./...

## errors: generate errors code
.PHONY: errors
errors: tools.verify.protoc-gen-gin-errors
	protoc --proto_path=./api \
          --proto_path=./third_party \
          --go_out=paths=source_relative:./api \
          --gin-errors_out=paths=source_relative:./api \
          $(ERROR_PROTO_FILES)

## docs: generate docs proto
.PHONY: docs
docs: tools.verify.protoc-gen-doc
	@mkdir -p docs/$(APP_NAME)
	protoc --proto_path=./api \
					--proto_path=./third_party \
					--go_out=paths=source_relative:./api \
					--doc_out=./docs/$(APP_NAME) \
					--doc_opt=html,index.html \
					$(API_PROTO_FILES)

.PHONY: openapi
# generate openapi
openapi: tools.verify.protoc-gen-openapi
	protoc --proto_path=./api \
	       --proto_path=./third_party \
	       --openapi_out=naming=proto,fq_schema_naming=true,default_response=false:. \
	       $(ALL_PROTO_FILES)
	@gin-ext swagger split

.PHONY: swagger
# generate swagger
swagger: tools.verify.protoc-gen-openapiv2 tools.verify.gin-ext
	@echo "deprecated, please use openapi instead."
	# mkdir -p $(ROOT_DIR)/docs/swagger
	# protoc --proto_path=./api \
	#         --proto_path=./third_party \
	#         --openapiv2_out ./docs/swagger \
	#         --openapiv2_opt logtostderr=true \
    #        $(ALL_PROTO_FILES)
	# @gin-ext swagger merge

## help-help: show help
help-help:
	@echo ''
	@echo 'Usage:'
	@echo ' make [target]'
	@echo ''
	@echo 'Targets:'
	@awk '/^[a-zA-Z\-_0-9]+:/ { \
	helpMessage = match(lastLine, /^## (.*)/); \
		if (helpMessage) { \
			helpCommand = substr($$1, 0, index($$1, ":")-1); \
			helpMessage = substr(lastLine, RSTART + 2, RLENGTH); \
			printf "\033[36m%-22s\033[0m %s\n", helpCommand,helpMessage; \
		} \
	} \
	{ lastLine = $$0 }' $(MAKEFILE_LIST)

.DEFAULT_GOAL := help

## wire: generate wire code
.PHONY: wire
wire: tools.verify.wire
	cd internal/$(APP_NAME)/server && wire

## genproto: generate proto file
.PHONY: genproto.%
genproto.%: tools.verify.gin-ext
	@gin-ext proto add api/$(APP_NAME)/v1/$*.proto

## genservice: generate service file
.PHONY: genservice.%
genservice.%: tools.verify.gin-ext
	@gin-ext proto server api/$(APP_NAME)/v1/$*.proto -t internal/$(APP_NAME)/service

## gencode: generate code file
.PHONY: gencode.%
gencode.%: tools.verify.gin-ext
	@gin-ext proto errcode api/$(APP_NAME)/v1/error_reason.proto -s $*
	@$(MAKE) errors
	@$(MAKE) docs
	@$(MAKE) api
	@gin-ext proto server api/$(APP_NAME)/v1/$*.proto -t internal/$(APP_NAME)/service
	@gin-ext proto biz api/$(APP_NAME)/v1/$*.proto -t internal/$(APP_NAME)/biz
	@gin-ext proto repo api/$(APP_NAME)/v1/$*.proto -t internal/$(APP_NAME)/data
	@$(MAKE) openapi
	@$(MAKE) wire
	@$(MAKE) format
	@$(MAKE) lint

## gencode: generate api file
.PHONY: genapi
genapi: tools.verify.gin-ext
	@$(MAKE) errors
	@$(MAKE) docs
	@$(MAKE) api
	@$(MAKE) openapi
	@$(MAKE) wire
	@$(MAKE) format
	@$(MAKE) lint

.PHONY: echo.gin
echo.gin:
	@echo "GOHOSTOS: $(GOHOSTOS)"
	@echo "APP_NAME: $(APP_NAME)"
	@echo "API_PROTO_FILES: $(API_PROTO_FILES)"
	